package com.teaching.teachingsupport.service.impl.studentserviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teaching.teachingsupport.common.pojo.dataobject.*;
import com.teaching.teachingsupport.mapper.*;
import com.teaching.teachingsupport.mapper.TeacherMapper;
import com.teaching.teachingsupport.mapper.studentviewmapper.CourseMapper;
import com.teaching.teachingsupport.mapper.studentviewmapper.SemesterMapper;
import com.teaching.teachingsupport.mapper.studentviewmapper.StudentCourseMapper;
import com.teaching.teachingsupport.mapper.studentviewmapper.StudentMapper;
import com.teaching.teachingsupport.mapper.teacherviewmapper.TeacherCourseMapper;
import com.teaching.teachingsupport.service.service2.studentservice.StudentCourseService;
import com.teaching.teachingsupport.common.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 学生课程服务实现类
 */
@Service
@RequiredArgsConstructor
public class StudentCourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements StudentCourseService {

    private final StudentMapper studentMapper;
    private final TeacherMapper teacherMapper;
    private final StudentCourseMapper studentCourseMapper;
    private final TeacherCourseMapper teacherCourseMapper;
    private final SemesterMapper semesterMapper;
    private final UserMapper userMapper;
    private final SecurityUtils securityUtils;

    @Override
    public List<Course> getStudentCourses(Long userId) {
        // 获取学生信息
        Student student = studentMapper.selectOne(
            new LambdaQueryWrapper<Student>()
                .eq(Student::getUserId, userId)
        );

        if (student == null) {
            return List.of();
        }

        // 获取学生选课信息
        List<StudentCourse> studentCourses = studentCourseMapper.selectList(
            new LambdaQueryWrapper<StudentCourse>()
                .eq(StudentCourse::getStudentId, student.getUserId())
        );

        // 获取课程信息并组装数据
        return studentCourses.stream().map(sc -> {
            Course course = this.getById(sc.getCourseId());
            if (course == null) {
                return null;
            }
            
            TeacherCourse teacherCourse = teacherCourseMapper.selectOne(
                new LambdaQueryWrapper<TeacherCourse>()
                    .eq(TeacherCourse::getCourseId, sc.getCourseId())
                    .eq(TeacherCourse::getSemesterId, sc.getSemesterId())
            );

            if (teacherCourse != null) {
                Teacher teacher = teacherMapper.selectById(teacherCourse.getTeacherId());
                if (teacher != null) {
                    User teacherUser = userMapper.selectById(teacher.getUserId());
                    if (teacherUser != null) {
                        course.setTeacherName(teacher.getTitle() + " " + teacherUser.getUsername());
                    }
                }

                Semester semester = semesterMapper.selectById(sc.getSemesterId());
                if (semester != null) {
                    course.setSemester(semester.getTermName() + " " + teacherCourse.getClassTime());
                }

                course.setStatus(teacherCourse.getStatus());
            }

            return course;
        })
        .filter(course -> course != null)
        .collect(Collectors.toList());
    }

    @Override
    public List<Course> getCurrentSemesterCourses(Long userId) {
        // 获取当前学期
        Semester currentSemester = semesterMapper.selectOne(
            new LambdaQueryWrapper<Semester>()
                .eq(Semester::getIsCurrent, 1)
        );

        if (currentSemester == null) {
            return List.of();
        }

        // 获取学生信息
        Student student = studentMapper.selectOne(
            new LambdaQueryWrapper<Student>()
                .eq(Student::getUserId, userId)
        );

        if (student == null) {
            return List.of();
        }

        // 获取当前学期的选课信息
        List<StudentCourse> studentCourses = studentCourseMapper.selectList(
            new LambdaQueryWrapper<StudentCourse>()
                .eq(StudentCourse::getStudentId, student.getUserId())
                .eq(StudentCourse::getSemesterId, currentSemester.getSemesterId())
        );

        // 获取课程信息并组装数据
        return studentCourses.stream().map(sc -> {
            Course course = this.getById(sc.getCourseId());
            if (course == null) {
                return null;
            }
            
            TeacherCourse teacherCourse = teacherCourseMapper.selectOne(
                new LambdaQueryWrapper<TeacherCourse>()
                    .eq(TeacherCourse::getCourseId, sc.getCourseId())
                    .eq(TeacherCourse::getSemesterId, currentSemester.getSemesterId())
            );

            if (teacherCourse != null) {
                Teacher teacher = teacherMapper.selectById(teacherCourse.getTeacherId());
                if (teacher != null) {
                    User teacherUser = userMapper.selectById(teacher.getUserId());
                    if (teacherUser != null) {
                        course.setTeacherName(teacher.getTitle() + " " + teacherUser.getUsername());
                    }
                }

                course.setSemester(currentSemester.getTermName() + " " + teacherCourse.getClassTime());
                course.setStatus(teacherCourse.getStatus());
            }

            return course;
        })
        .filter(course -> course != null)
        .collect(Collectors.toList());
    }
}